home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / e33el2.zip / emacs / 19.33 / lisp / lmenu.el < prev    next >
Lisp/Scheme  |  1996-07-02  |  19KB  |  507 lines

  1. ;;; lmenu.el --- emulate Lucid's menubar support
  2.  
  3. ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Keywords: emulations
  6.  
  7. ;; This file is part of GNU Emacs.
  8.  
  9. ;; GNU Emacs is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  21. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. ;; Boston, MA 02111-1307, USA.
  23.  
  24. ;;; Code:
  25.  
  26.  
  27. ;; First, emulate the Lucid menubar support in GNU Emacs 19.
  28.  
  29. ;; Arrange to use current-menubar to set up part of the menu bar.
  30.  
  31. (defvar current-menubar)
  32.  
  33. (setq recompute-lucid-menubar 'recompute-lucid-menubar)
  34. (defun recompute-lucid-menubar ()
  35.   (define-key lucid-menubar-map [menu-bar]
  36.     (condition-case nil
  37.     (make-lucid-menu-keymap "menu-bar" current-menubar)
  38.       (error (message "Invalid data in current-menubar moved to lucid-failing-menubar")
  39.          (sit-for 1)
  40.          (setq lucid-failing-menubar current-menubar
  41.            current-menubar nil))))
  42.   (setq lucid-menu-bar-dirty-flag nil))
  43.  
  44. (defvar lucid-menubar-map (make-sparse-keymap))
  45. (or (assq 'current-menubar minor-mode-map-alist)
  46.     (setq minor-mode-map-alist
  47.       (cons (cons 'current-menubar lucid-menubar-map)
  48.         minor-mode-map-alist)))
  49.  
  50. (defun set-menubar-dirty-flag ()
  51.   (force-mode-line-update)
  52.   (setq lucid-menu-bar-dirty-flag t))
  53.  
  54. (defvar add-menu-item-count 0)
  55.  
  56. ;; This is a variable whose value is always nil.
  57. (defvar make-lucid-menu-keymap-disable nil)
  58.  
  59. ;; Return a menu keymap corresponding to a Lucid-style menu list
  60. ;; MENU-ITEMS, and with name MENU-NAME.
  61. (defun make-lucid-menu-keymap (menu-name menu-items)
  62.   (let ((menu (make-sparse-keymap menu-name)))
  63.     ;; Process items in reverse order,
  64.     ;; since the define-key loop reverses them again.
  65.     (setq menu-items (reverse menu-items))
  66.     (while menu-items
  67.       (let ((item (car menu-items))
  68.         command name callback)
  69.     (cond ((stringp item)
  70.            (setq command nil)
  71.            (setq name (if (string-match "^-+$" item) "" item)))
  72.           ((consp item)
  73.            (setq command (make-lucid-menu-keymap (car item) (cdr item)))
  74.            (setq name (car item)))
  75.           ((vectorp item)
  76.            (setq command (make-symbol (format "menu-function-%d"
  77.                           add-menu-item-count))
  78.              add-menu-item-count (1+ add-menu-item-count)
  79.              name (aref item 0)
  80.              callback (aref item 1))
  81.            (if (symbolp callback)
  82.            (fset command callback)
  83.          (fset command (list 'lambda () '(interactive) callback)))
  84.            (put command 'menu-alias t)
  85.            (let ((i 2))
  86.          (while (< i (length item))
  87.            (cond
  88.             ((eq (aref item i) ':active)
  89.              (put command 'menu-enable
  90.               (or (aref item (1+ i))
  91.                   'make-lucid-menu-keymap-disable))
  92.              (setq i (+ 2 i)))
  93.             ((eq (aref item i) ':suffix)
  94.              ;; unimplemented
  95.              (setq i (+ 2 i)))
  96.             ((eq (aref item i) ':keys)
  97.              ;; unimplemented
  98.              (setq i (+ 2 i)))
  99.             ((eq (aref item i) ':style)
  100.              ;; unimplemented
  101.              (setq i (+ 2 i)))
  102.             ((eq (aref item i) ':selected)
  103.              ;; unimplemented
  104.              (setq i (+ 2 i)))
  105.             ((and (symbolp (aref item i))
  106.               (= ?: (string-to-char (symbol-name (aref item i)))))
  107.              (error "Unrecognized menu item keyword: %S"
  108.                 (aref item i)))
  109.             ((= i 2)
  110.              ;; old-style format: active-p &optional suffix
  111.              (put command 'menu-enable
  112.               (or (aref item i) 'make-lucid-menu-keymap-disable))
  113.              ;; suffix is unimplemented
  114.              (setq i (length item)))
  115.             (t
  116.              (error "Unexpected menu item value: %S"
  117.                 (aref item i))))))))
  118.     (if (null command)
  119.         ;; Handle inactive strings specially--allow any number
  120.         ;; of identical ones.
  121.         (setcdr menu (cons (list nil name) (cdr menu)))
  122.       (if name
  123.           (define-key menu (vector (intern name)) (cons name command)))))
  124.       (setq menu-items (cdr menu-items)))
  125.     menu))
  126.  
  127. (defun popup-menu (menu-desc)
  128.   "Pop up the given menu.
  129. A menu is a list of menu items, strings, and submenus.
  130.  
  131. The first element of a menu must be a string, which is the name of the
  132. menu.  This is the string that will be displayed in the parent menu, if
  133. any.  For toplevel menus, it is ignored.  This string is not displayed
  134. in the menu itself.
  135.  
  136. A menu item is a vector containing:
  137.  
  138.  - the name of the menu item (a string);
  139.  - the `callback' of that item;
  140.  - a list of keywords with associated values:
  141.    - :active active-p    a form specifying whether this item is selectable;
  142.    - :suffix suffix    a string to be appended to the name as an `argument'
  143.             to the command, like `Kill Buffer NAME';
  144.    - :keys command-keys    a string, suitable for `substitute-command-keys',
  145.             to specify the keyboard equivalent of a command
  146.             when the callback is a form (this is not necessary
  147.             when the callback is a symbol, as the keyboard
  148.             equivalent is computed automatically in that case);
  149.    - :style style    a symbol: nil for a normal menu item, `toggle' for
  150.             a toggle button (a single option that can be turned
  151.             on or off), or `radio' for a radio button (one of a
  152.             group of mutually exclusive options);
  153.    - :selected form    for `toggle' or `radio' style, a form that specifies
  154.             whether the button will be in the selected state.
  155.  
  156. Alternately, the vector may contain exactly 3 or 4 elements, with the third
  157. element specifying `active-p' and the fourth specifying `suffix'.
  158.  
  159. If the `callback' of a menu item is a symbol, then it must name a command.
  160. It will be invoked with `call-interactively'.  If it is a list, then it is
  161. evaluated with `eval'.
  162.  
  163. If an element of a menu is a string, then that string will be presented in
  164. the menu as unselectable text.
  165.  
  166. If an element of a menu is a string consisting solely of hyphens, then that
  167. item will be presented as a solid horizontal line.
  168.  
  169. If an element of a menu is a list, it is treated as a submenu.  The name of
  170. that submenu (the first element in the list) will be used as the name of the
  171. item representing this menu on the parent.
  172.  
  173. The syntax, more precisely:
  174.  
  175.    form        :=  <something to pass to `eval'>
  176.    command    :=  <a symbol or string, to pass to `call-interactively'>
  177.    callback     :=  command | form
  178.    active-p    :=  <t or nil, whether this thing is selectable>
  179.    text        :=  <string, non selectable>
  180.    name        :=  <string>
  181.    suffix    :=  <string>
  182.    command-keys    :=  <string>
  183.    object-style    :=  'nil' | 'toggle' | 'radio'
  184.    keyword    :=  ':active' active-p
  185.          |  ':suffix' suffix
  186.          |  ':keys' command-keys
  187.          |  ':style' object-style
  188.          |  ':selected' form
  189.    menu-item    :=  '['  name callback active-p [ suffix ]  ']'
  190.          |  '['  name callback [ keyword ]+  ']'
  191.    menu        :=  '(' name [ menu-item | menu | text ]+ ')'"
  192.   (let ((menu (make-lucid-menu-keymap (car menu-desc) (cdr menu-desc)))
  193.     (pos (mouse-pixel-position))
  194.     answer cmd)
  195.     (while (and menu
  196.         (setq answer (x-popup-menu (list (list (nth 1 pos)
  197.                                (nthcdr 2 pos))
  198.                          (car pos))
  199.                        menu)))
  200.       (setq cmd (lookup-key menu (apply 'vector answer)))
  201.       (setq menu nil)
  202.       (and cmd
  203.        (if (keymapp cmd)
  204.            (setq menu cmd)
  205.          (call-interactively cmd))))))
  206.  
  207. (defun popup-dialog-box (data)
  208.   "Pop up a dialog box.
  209. A dialog box description is a list.
  210.  
  211.  - The first element of the list is a string to display in the dialog box.
  212.  - The rest of the elements are descriptions of the dialog box's buttons.
  213.    Each one is a vector of three elements:
  214.    - The first element is the text of the button.
  215.    - The second element is the `callback'.
  216.    - The third element is t or nil, whether this button is selectable.
  217.  
  218. If the `callback' of a button is a